package ch.supsi.parentalcontrol;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import ch.supsi.mobile.core.Address;
import ch.supsi.mobile.core.ICoreApplicationsManager;
import ch.supsi.mobile.core.Page;
import ch.supsi.mobile.core.applications.AddressesApplication;
import ch.supsi.mobile.core.applications.BrowserApplication;

public class Grafica extends JFrame{

	// dichiaro gli elementi che utilizzo per realizzare la grafica
	private GridBagConstraints containerLayout = new GridBagConstraints();
	
	// la barra del menu
	private JMenuBar menuBar;
	private JMenu file, edit;
	private JMenuItem about, exit;
	
	// il pannello e le sue schede
	private JTabbedPane schedario;
	private JPanel application, login;
	private AdminPanel phoneA, addressA, messageA, browserA;
	private UserPanel phoneU, addressU, messageU, browserU;
		
	// gli oggetti per la schedina del login
	private JTextField userName;
	private JPasswordField password;
	private JLabel insertUserName, insertPassword;
	private JButton loginOK;
	
	// gli elementi per le application
	private JLabel phoneLabel, addressLabel, messageLabel, browserLabel, administrationLabel;
	private JCheckBox phoneCheck, addressCheck, messageCheck, browserCheck;
	private static JRadioButton whiteListRadio, blackListRadio;
	private ButtonGroup listGroup;
	private JPanel radioPanel;
	
	// dichiaro gli Application per le Liste
	AddressesApplication AA;
	BrowserApplication BA;
	ICoreApplicationsManager coreAppManager;
	
	/** costruttore */ 
	public Grafica(ICoreApplicationsManager cam){
		initComponent(cam);
		setVisible(true);
        pack();
	}
	
	/** realizzazione della grafica */
	private void initComponent(ICoreApplicationsManager cam){
		// inizializzazione elementi precedentemente dichiarati
        menuBar = new JMenuBar();
        file = new JMenu();
        edit = new JMenu();
        about = new JMenuItem();
        exit = new JMenuItem();
	    schedario = new JTabbedPane();
	    
	    AA = (AddressesApplication) cam.getAddressesApplication();
	    BA = (BrowserApplication) cam.getBrowserApplication();
	    
	    // riempio l'array degli address
	    Address indirizzoW = new Address("id1","Giuseppe Rossi","0918256635");
	    Address indirizzoB = new Address("id2","Franco Neri","0918255432");
	    AA.addNewAddress(indirizzoW);
	    AA.addBlackAddress(indirizzoB);
	    
	    Page pageW = new Page("www.google.ch", "Motore di ricerca");
	    Page pageB = new Page("www.maps.ch", "Ricerca mappe");
	    BA.addPage(pageW);
	    BA.addBlackPage(pageB);
	    
	  //phoneA = new AdminPanel(BA.getAllPage());
        addressA = new AdminPanel(AA.getWhiteListModel(), AA.getBlackListModel(), "Address");
        //messageA = new AdminPanel();
        browserA = new AdminPanel(BA.getWhiteListModel(), BA.getBlackListModel(), "Browser");
        
        //phoneU = new UserPanel();
        addressU = new UserPanel(AA.getCompleteListModel(), AA.getWhiteListModel(), AA.getBlackListModel(), "address");
        //messageU = new UserPanel();
        browserU = new UserPanel(BA.getCompleteListModel(), BA.getWhiteListModel(), BA.getBlackListModel(), "browser");
        
        
        login = new JPanel();
        userName = new JTextField();
        password = new JPasswordField();
        insertUserName = new JLabel();
        insertPassword = new JLabel();
        loginOK = new JButton();
        
        application = new JPanel();
        phoneCheck = new JCheckBox();
        addressCheck = new JCheckBox();
        messageCheck = new JCheckBox();
        browserCheck = new JCheckBox();
        phoneLabel = new JLabel();
        addressLabel = new JLabel();
        messageLabel = new JLabel();
        browserLabel = new JLabel();
        
        whiteListRadio = new JRadioButton();
        blackListRadio = new JRadioButton();
        listGroup = new ButtonGroup();
        administrationLabel = new JLabel();
        radioPanel = new JPanel();
                
        // tramite questo comado faccio in modo che alla chiusura del programma
        // anche il processo si fermi
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        
        // dimensioni minime finestra
        this.setMinimumSize(new Dimension(1000, 500));
        
        // titolo applicazione
        this.setTitle("Parental Control");
        
        // creazione e aggiunta della barra del menu
        menuBar.setBackground(new Color(115, 172, 236));
        file.setText("File");
        exit.setText("Exit");
        exit.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
        edit.setText("Edit");
        about.setText("About");
        about.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null, "Parental Control @ 2011\n\nDeveloped by Catia Guidi, Luca Bovino", "Information", JOptionPane.INFORMATION_MESSAGE);
			}
		});
        file.add(exit);
        edit.add(about);
        menuBar.add(file);
        menuBar.add(edit);
        setJMenuBar(menuBar);
        
        /****************/
        /** TAB PHONE */
        /*schedario.addTab("Phone Admin", phoneA);
        schedario.addTab("Phone", phoneU);
        schedario.setEnabledAt(0, false);*/
        
        /****************/
        /** TAB ADDRESS */
        schedario.addTab("Address Admin", addressA);
        schedario.addTab("Address", addressU);
        // schedario.setEnabledAt(2, false); => con phone
        // schedario.setEnabledAt(0, false);

        /****************/
        /** TAB MESSAGE */
        /*schedario.addTab("Message Admin", messageA);        
        schedario.addTab("Message", messageU);
        schedario.setEnabledAt(4, false);*/

        /****************/
        /** TAB BROWSER */
        schedario.addTab("Browser Admin", browserA);
        schedario.addTab("Browser", browserU);
        // schedario.setEnabledAt(6, false); => con phone e message
        // schedario.setEnabledAt(2, false); 
        
        /**************/
        /** TAB LOGIN */
        // inserisco la scheda di login
        schedario.addTab("Login", login);
        
        // setto il layout per la scheda del login
        GridBagLayout loginLayout = new GridBagLayout();
        login.setLayout(loginLayout);
        
        // inserisco il label per UserName
        insertUserName.setText("UserName");
        containerLayout.gridx = 0;
        containerLayout.gridy = 0;
        containerLayout.ipadx = 50;
        containerLayout.ipady = 25;
        login.add(insertUserName, containerLayout);

        // inserisco il textfiel nel quale verrà poi inserito il nome utente
        userName.setPreferredSize(new Dimension(150, 10));
        containerLayout.gridx = 1;
        containerLayout.gridy = 0;
        login.add(userName, containerLayout);

        // aggiugno il label Password
        containerLayout.gridx = 0;
        containerLayout.gridy = 2;
        containerLayout.ipadx = 50;
        login.add(insertPassword, containerLayout);

        // inserisco il passwordfield nel quale l'amministartore inserir� la password
        password.setPreferredSize(new Dimension(150, 10));
        containerLayout.gridx = 1;
        containerLayout.gridy = 2;
        login.add(password, containerLayout);
        
        // aggiungo il bottone per confermare il login
        loginOK.setText("Login");
        containerLayout.gridx = 1;
        containerLayout.gridy = 4;
        containerLayout.ipadx = 50;
        containerLayout.ipady = 10;
        loginOK.addActionListener(new GestioneSchedaLogin(this));
        login.add(loginOK, containerLayout);

        /********************/
        /** TAB APPLICATION */
        // immetto la scheda application nello schedario
        schedario.addTab("Administration", application);
        // schedario.setEnabledAt(9, false); => con phone e message
        // schedario.setEnabledAt(5, false);
        
        // setto il layout all scheda
        GridBagLayout applicationLayout = new GridBagLayout();
        application.setLayout(applicationLayout);

        // inserisco il check Box e il testo per il telefono
        /*phoneCheck.setText("Phone");
        phoneCheck.setSelected(true);
        containerLayout.gridx = 0;
        containerLayout.gridy = 0;
        containerLayout.fill = GridBagConstraints.HORIZONTAL;
        containerLayout.ipadx = 100;
        containerLayout.ipady = 25;
        application.add(phoneCheck, containerLayout);

        phoneLabel.setText("Abilita / Disabilita la possibilità di visionare i numeri di telefono");
        containerLayout.gridx = 2;
        containerLayout.gridy = 0;
        containerLayout.ipady = 25;
        application.add(phoneLabel, containerLayout);*/
                
        // inserisco la scelta del metodo da usare
        whiteListRadio.setText("Use white list");
        whiteListRadio.setSelected(true);
        containerLayout.gridx = 0;
        containerLayout.gridy = 1;
        containerLayout.fill = GridBagConstraints.HORIZONTAL;
        containerLayout.ipadx = 100;
        containerLayout.ipady = 25;
        //application.add(whiteListRadio, containerLayout);
        
        blackListRadio.setText("Use black list");
        blackListRadio.setSelected(false);
        containerLayout.gridx = 0;
        containerLayout.gridy = 2;
        containerLayout.fill = GridBagConstraints.HORIZONTAL;
        containerLayout.ipadx = 100;
        containerLayout.ipady = 25;
        //application.add(blackListRadio, containerLayout);
        
        listGroup.add(whiteListRadio);
        listGroup.add(blackListRadio);
        radioPanel.setLayout(new GridLayout(2, 1));
        radioPanel.add(whiteListRadio);
        radioPanel.add(blackListRadio);
        application.add(radioPanel, containerLayout);

        administrationLabel.setText("Determina quale implementazione usare");
        containerLayout.gridx = 2;
        containerLayout.gridy = 2;
        containerLayout.ipady = 25;
        application.add(administrationLabel, containerLayout);
        
        // inserisco la scelta per l'address
        addressCheck.setText("Address");
        addressCheck.setSelected(true);
        addressCheck.addActionListener(new GestioneSchedaAdministration(this));
        containerLayout.gridx = 0;
        containerLayout.gridy = 3;
        containerLayout.fill = GridBagConstraints.HORIZONTAL;
        containerLayout.ipadx = 100;
        containerLayout.ipady = 25;
        application.add(addressCheck, containerLayout);

        addressLabel.setText("Abilita / Disabilita la possibilità di visionare gli indirizzi");
        containerLayout.gridx = 2;
        containerLayout.gridy = 3;
        containerLayout.ipady = 25;
        application.add(addressLabel, containerLayout);

        // aggiungo il CheckBox e il testo per i messaggi
        /*messageCheck.setText("Message");
        messageCheck.setSelected(true);
        containerLayout.gridx = 0;
        containerLayout.gridy = 4;
        containerLayout.fill = GridBagConstraints.HORIZONTAL;
        containerLayout.ipadx = 100;
        containerLayout.ipady = 25;
        application.add(messageCheck, containerLayout);

        messageLabel.setText("Abilita / Disabilita la possibilità di visionare i messaggi");
        containerLayout.gridx = 2;
        containerLayout.gridy = 4;
        containerLayout.ipady = 25;
        application.add(messageLabel, containerLayout);*/

        // inserisco l'opzione di scelta per il browser
        browserCheck.setText("Browser");
        browserCheck.setSelected(true);
        browserCheck.addActionListener(new GestioneSchedaAdministration(this));
        containerLayout.gridx = 0;
        containerLayout.gridy = 6;
        containerLayout.fill = GridBagConstraints.HORIZONTAL;
        containerLayout.ipadx = 100;
        containerLayout.ipady = 25;
        application.add(browserCheck, containerLayout);
        
        browserLabel.setText("Abilita / Disabilita la possibilità di visionare le pagine web");
        containerLayout.gridx = 2;
        containerLayout.gridy = 6;
        containerLayout.ipady = 25;
        application.add(browserLabel, containerLayout);

        for (int i = 0; i < schedario.getTabCount(); i++) {
			if (schedario.getTitleAt(i).equals("Address Admin")
					|| schedario.getTitleAt(i).equals("Browser Admin")
					|| schedario.getTitleAt(i)
							.equals("Administration")) {
				schedario.setEnabledAt(i, false);
			}
		}
        
        //tab aperta all'inizio
        schedario.setSelectedComponent(login);
        
        // aggiungo lo schedario
        this.add(schedario);
	}
	
	public JTabbedPane getSchedario() {
		return schedario;
	}

	public JTextField getUserName() {
		return userName;
	}

	public JPasswordField getPassword() {
		return password;
	}

	public JButton getLoginOK() {
		return loginOK;
	}

	public JCheckBox getPhoneCheck() {
		return phoneCheck;
	}

	public JCheckBox getAddressCheck() {
		return addressCheck;
	}

	public JCheckBox getMessageCheck() {
		return messageCheck;
	}

	public JCheckBox getBrowserCheck() {
		return browserCheck;
	}

	public static JRadioButton getWhiteListRadio() {
		return whiteListRadio;
	}

	public static JRadioButton getBlackListRadio() {
		return blackListRadio;
	}
	
}
